package com.forest.util;

import com.forest.constants.EncryptionAlgorithm;
import com.forest.service.AesEncryptionImpl;
import com.forest.service.IEncryption;
import com.forest.service.Sm4EncryptionImpl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 加密管理
 *
 * @author Forest
 */
public class EncryptionManager {

    /** 数据加密算法实现 */
    private static final Map<EncryptionAlgorithm, IEncryption> ENCRYPTION_ALGORITHM = new ConcurrentHashMap<>();

    static {
        ENCRYPTION_ALGORITHM.put(EncryptionAlgorithm.SM4, new Sm4EncryptionImpl());
        ENCRYPTION_ALGORITHM.put(EncryptionAlgorithm.AES, new AesEncryptionImpl());
    }

    /**
     * 获取加密算法实现
     *
     * @param algorithm
     * @return
     */
    public static IEncryption getEncryption(EncryptionAlgorithm algorithm) {
        return ENCRYPTION_ALGORITHM.getOrDefault(algorithm, new Sm4EncryptionImpl());
    }

    /**
     * 获取加密算法实现
     *
     * @param algorithm
     * @return
     */
    public static IEncryption getEncryption(String algorithm) {
        return getEncryption(EncryptionAlgorithm.getByAlgorithm(algorithm));
    }
}
